import { ref } from 'vue'
import { fetchPointsRecords, fetchAllPointsRecords } from '../axioser'

export function usePoints() {
  // 积分基础设置
  const pointsOptions = {
    //   记录ID
    record_id: '',
    //   操作者
    operater: {
      type: undefined,
      uid: '',
      name: '',
      _wx: '',
    },
    // 被操作者
    uid: '',
    name: '',
    create: '',
    _wx: '',
    note: '',
    state: {
      type: undefined,
      count: 0,
      after_change: 0,
      before_change: 0,
    },
    source: {
      type: undefined,
      detail: '',
    },
  }
  // 全部积分变动列表
  const allPointsRecords = ref({
    list: [],
    currentIndex: 1,
    total: 0,
    pageSize: 0,
    over: false,
  })

  // 获取某个会员的积分变动记录
  const getPointsRecords = async (uid = undefined, page = 1, _result = 10) => {
    try {
      const {
        data: { data },
      } = await fetchPointsRecords({ uid, page, _result })
      allPointsRecords.value = data
    } catch (error) {
      console.error(error)
    }
  }
  //  获取全部积分变动记录
  const getAllPointsRecords = async (page = 1, _result = 10) => {
    try {
      const {
        data: { data },
      } = await fetchAllPointsRecords({ page, _result })
      allPointsRecords.value = data
    } catch (error) {
      console.error(error)
    }
  }

  return {
    pointsOptions,
    allPointsRecords,
    getAllPointsRecords,
  }
}
